package com.czd.support.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.support.common.Configuration;
import com.czd.support.model.Manager;
import com.czd.support.model.Talk;
import com.czd.support.service.ReplyService;
import com.czd.support.service.TalkService;

@Controller
@RequestMapping("/talk")
public class TalkController extends BaseController {

	@Autowired
	private TalkService talkService;
	
	@Autowired
	private  ReplyService  replyService  ;
	

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Talk talk, HttpServletRequest request, Model model,
			@RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
		Manager manager = this.getSesionUser(request);
		if (manager != null) {
			talk.setPageSize(Configuration.getInstance().getDefault_page_size());
			talk.setPageNo(pageNo);
			Paginate queryForPage = talkService.queryForPage(talk);
			model.addAttribute("paginate", queryForPage);
			return "talk/list";
		} else {
			return "redirect:/login";
		}
	}
	/**\
	 * 
	 * 删除参与讨论
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable int id, Model model){
//		Talk talk = talkService.findById(id); 
		talkService.deleteById(id);
		//查询是否有reply信息
		int findByTidCount = replyService.findByTidCount(id);
		if(findByTidCount!=0){
			replyService.deleteByTid(id);
		}
		return "redirect:/talk/list";
	}

}
